home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / keymover / keymover.pas < prev    next >
Pascal/Delphi Source File  |  1995-12-22  |  5KB  |  138 lines

  1. {---------------------------------------------------------------------------
  2.  
  3.        PROJECT: DataControl extensions
  4.          TITLE: TTableKeyMover
  5.       SUBTITLE:
  6.  
  7.        VERSION: 1.00
  8.  
  9.   DATE STARTED: 1st June 1995
  10.   DATE UPDATED:
  11.  
  12.         AUTHOR: (c) UK 1995 Matthew Page.
  13.                 All Rights Reserved
  14.  
  15.         This code is released as FREEWARE, and if it goes wrong,
  16.         don't blame me!
  17.                         ** Enjoy! **
  18.  
  19.                 "Keep those Delphi components coming and let's
  20.                  smash VB into the ground!"
  21.  
  22.          NOTES: This object was designed for forms created with the
  23.                 database form expert. It allows keyboard naviagtion
  24.                 of the table requested with the PgUp and PgDn keys.
  25.                 Shift key combinations will move by MoveBy records,
  26.                 and Ctrl key combinations will take you to the first
  27.                 and the last records of the table.
  28.  
  29.                 Ensure that the Forms KeyPreview is True to use.
  30.                 Simply slap this non-visual component on the form
  31.                 and hook up the DataSet value, and that's it!!
  32.  
  33.      TECHNICAL: You will notice that only the Form can have a KeyPreview
  34.                 property, so you need to 'hack' it in with messages.
  35.                 To do this, this object assigns its own OnKeyDown event
  36.                 to whatever the form had. It then hooks in HandleKeyDown
  37.                 into the Forms OnKeyDown event. Hey presto! That's it.
  38.                 The HandleKeyDown checks the keys, and responds appropriately,
  39.                 navigating the table, even if the current focus is on a
  40.                 field.
  41.  
  42.                 One slight piece of interest: The Destroy event must
  43.                 unhook the event, otherwise deleting the visual component
  44.                 will cause a GPF because the Form's OnKeyDown would point
  45.                 into the wild blue yonder of the unitialised heap!
  46.                 (It caught me out on the first attempt!!!!)
  47.  
  48. ----------------------------------------------------------------------------}
  49. unit Keymover;
  50.  
  51. interface
  52.  
  53. uses
  54.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  55.   Forms, Dialogs, DBTables, DB;
  56.  
  57. type
  58.   TTableKeyMover = class(TComponent)
  59.   private
  60.     { Private declarations }
  61.     VNoMove : INTEGER;
  62.     VTable : TTable;
  63.     FOnKeyDown : TKeyEvent;
  64.     TheForm    : TForm;
  65.   protected
  66.     { Protected declarations }
  67.   public
  68.     { Public declarations }
  69.     constructor Create(AOwner : TComponent); override;
  70.     destructor Destroy; override;
  71.     procedure HandleKeyDown(Sender : TObject;
  72.                             var Key : WORD; Shift : TShiftState);
  73.   published
  74.     { Published declarations }
  75.     property DataSet : TTable read VTable write VTable;
  76.     property OnKeyDown : TKeyEvent read FOnKeyDown write FOnKeyDown;
  77.     property MoveBy : INTEGER read VNoMove write VNoMove default 5;
  78.   end;
  79.  
  80. procedure Register;
  81.  
  82. implementation
  83.  
  84. constructor TTableKeyMover.Create(AOwner : TComponent);
  85. begin
  86.  inherited Create(AOwner);
  87.  IF AOwner is TForm                          {Just checking....}
  88.  THEN BEGIN
  89.        TheForm := AOwner as TForm;           {Keep this for later}
  90.        OnKeyDown := TheForm.OnKeyDown;       {Store the old}
  91.        TheForm.OnKeyDown := HandleKeyDown;   {Activate the new}
  92.       END;
  93.  MoveBy := 5;                                {To correspond to the default
  94.                                               setting in the methods}
  95. end;
  96.  
  97. destructor TTableKeyMover.Destroy;
  98. begin
  99.  TheForm.OnKeyDown := FOnKeyDown;            {Unhook the key handler}
  100.  inherited Destroy;                          {Carry on with the original}
  101. end;
  102.  
  103. procedure TTableKeyMover.HandleKeyDown(Sender : TObject;
  104.                             var Key : WORD; Shift : TShiftState);
  105. begin
  106.  IF Assigned(VTable)                         {Just checking...}
  107.  THEN BEGIN
  108.        CASE Key OF                           {Check key stroke and act}
  109.         vk_next  : BEGIN
  110.                     IF Shift = [ssShift]
  111.                     THEN VTable.MoveBy(MoveBy)
  112.                     ELSE
  113.                     IF Shift = [ssCtrl]
  114.                     THEN VTable.Last
  115.                     ELSE VTable.MoveBy(1);
  116.                     Key := 0;                 {Clear the key stroke}
  117.                    END;
  118.         vk_prior : BEGIN
  119.                     IF Shift = [ssShift]
  120.                     THEN VTable.MoveBy(-MoveBy)
  121.                     ELSE
  122.                     IF Shift = [ssCtrl]
  123.                     THEN VTable.First
  124.                     ELSE VTable.MoveBy(-1);
  125.                     Key := 0;
  126.                    END;
  127.         END;
  128.       END;                                     {Finally, do the old...}
  129.  IF Assigned(FOnKeyDown) THEN FOnKeyDown(Sender, Key, Shift);
  130. end;
  131.  
  132. procedure Register;
  133. begin
  134.   RegisterComponents('Data Controls', [TTableKeyMover]);
  135. end;
  136.  
  137. end.
  138.